package com.ghy.nba.controllers;


import com.ghy.nba.dao.PowerRepository;
import com.ghy.nba.entities.BallStar;
import com.ghy.nba.entities.Power;
import com.ghy.nba.services.BallstarService;
import com.ghy.nba.services.PowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("powers")
public class PowerControllers {

    @Autowired
    PowerService powerService;

    @Autowired
    BallstarService ballstarService;

    @GetMapping
    public String displayPower(Model model) {
        List<Power> powers = powerService.getAll();
        model.addAttribute("powers", powers);
        return "power/list-power";
    }

    @GetMapping("/new")
    public String displayPowerFrom(Model model) {
        Power apower = new Power();
        model.addAttribute("power", apower);

        return "power/new-power";
    }

    @PostMapping("/save")
    public String createPower(Model model, @Valid Power power, Errors errors) {
        if (errors.hasErrors())
            return "redirect:/powers/new";

        powerService.save(power);
        return "redirect:/powers";
    }


    @GetMapping("/update")
    public String displayPowerUpdateForm(@RequestParam("id")long theId, Model model){

        Power thePower = powerService.findByPowerId(theId);
        model.addAttribute("power",thePower);
        return "power/new-power";
    }

    @GetMapping("/delete")
    public String deletePower(@RequestParam("id") long theId, Model model) {

        Power thePower = powerService.findByPowerId(theId);
        powerService.delete(thePower);
        return "redirect:/powers";
    }

}